package mall

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"private-domain-overseas-service/app/business/api/internal/svc"

	"private-domain-overseas-service/pkg/common"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/hzmallgoods"
	"private-domain-overseas-service/tools/ent/predicate"
)

type MallGoodsDao struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMallGoodsDao(ctx context.Context, svcCtx *svc.ServiceContext) *MallGoodsDao {
	return &MallGoodsDao{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// MallGoodsConditionsPageAll - 分页查询海外商城推荐商品
func (l *MallGoodsDao) MallGoodsConditionsPageAll(page, pageSize int64, conditions []predicate.HzMallGoods) ([]*ent.HzMallGoods, int64, error) {
	offset := common.Offset(page, pageSize)
	hzMallGoods, err := l.svcCtx.Orm.HzMallGoods.Query().
		Where(conditions...).
		Offset(int(offset)).
		Limit(int(pageSize)).
		Order(ent.Desc(hzmallgoods.FieldGoodsCreatedAt)).
		All(l.ctx)
	if err != nil {
		return nil, 0, err
	}
	total, err := l.svcCtx.Orm.HzMallGoods.Query().Where(conditions...).Count(l.ctx)
	if err != nil {
		return nil, 0, err
	}
	return hzMallGoods, int64(total), nil

}

// MallGoodsConditionsFirst -根据条件查询单个商品
func (l *MallGoodsDao) MallGoodsConditionsFirst(conditions []predicate.HzMallGoods) (*ent.HzMallGoods, error) {
	hzMall, err := l.svcCtx.Orm.HzMallGoods.Query().Where(conditions...).First(l.ctx)
	if err != nil {
		return nil, err
	}
	return hzMall, nil

}
